package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.text.TextUtils
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.OrganizationActivity
import cc.vv.btpartyjob.jnimodule.enums.H5UrlEnum
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.WangGeListActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.resp.GridListResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.party.beans.vo.OrgVO
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 网格列表
 * Created by Chen.qi on 2018/10/25.
 */
class WangGeListActivity : BaseActivityMVP<WangGeListActivityDelegate>(), BaseQuickAdapter.OnItemClickListener {
    //社区id
    private var communityId: String? = null
    /**
     * 数据
     */
    private var mDataList: ArrayList<OrgVO>? = null

    override fun initData() {

        mDataList = ArrayList()
        var booleanExtra: Boolean
        try {
            communityId = intent.getStringExtra("communityId")
            booleanExtra = intent.getBooleanExtra("isShow", false)
        } catch (e: Exception) {
            booleanExtra = false
        }
        viewDelegate?.setTopBarTitle("网格", booleanExtra)
        if (TextUtils.isEmpty(communityId)) {
            val userInfo = UserInfoManageUtil.getUserInfo()
            val userOrgList = userInfo?.userOrgList
            if (null != userOrgList) {
                for (i in userOrgList.indices) {
                    if (userOrgList[i].type == 2) {
                        communityId = userOrgList[i].id
                        break
                    }
                }
            }
        }
        PartyManagerServer.getGridList(communityId + "", true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent(this@WangGeListActivity, OrganizationActivity::class.java)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 2)
                intent.putExtra(IntentJniKey.INTENT_KEY_ORG_CHOOSE, 2)
                startActivityForResult(intent, 100)
            }
        })
        viewDelegate.bindOnItemClick(this)

    }

    override fun getDelegateClass(): Class<WangGeListActivityDelegate> {
        return WangGeListActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        JniStartActivity.startIntentWebActivity(this, H5UrlEnum.网格详情, mDataList!![position].id)
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is GridListResponseObj -> {
                mDataList!!.clear()
                val data = obj.data
                if (null != data) {
                    mDataList!!.addAll(data)
                }
                viewDelegate.setAdapterData(mDataList)
            }
        }
    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        viewDelegate.setAdapterData(null)
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        viewDelegate.setAdapterData(null)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (null == data) {
            return
        }
        if (requestCode == 100 && resultCode == 101) {
            communityId = data.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            PartyManagerServer.getGridList(communityId + "", true)
        }
    }

}